#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>

using namespace std;

int main()
{
    int T;
    cin >> T;

    getchar();

    while (T--)
    {
        stack<char> st;
        string s;
        getline(cin, s);

        int len = (int)s.size();

        for (int i = 0;i < len;i++)
        {
            if (s[i] != ' ') st.push(s[i]);

            if (s[i] == ' ' || i == len - 1)
            {
                while (!st.empty())
                {
                    cout << st.top();
                    st.pop();
                }
                if (i != len - 1) cout << ' ';
            }
        }
        cout << endl;
    }
    return 0;
}
